package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加收货地址
func CreateAddress(c *gin.Context) {
	var data model.Address
	var code int
	_ = c.ShouldBindJSON(&data)
	code = model.CreateAddress(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 删除收货地址
func DeleteAddress(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteAddress(id)
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetErrMsg(code),
	})
}

// 获取收货地址列表
func GetAddressList(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	data, total := model.GetAddressList(id)
	code = errmsg.SUCCESS
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"data":    data,
		"total":   total,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 编辑收货地址
func UpdateAddress(c *gin.Context) {
	var code int
	var data model.Address
	c.ShouldBindJSON(&data)
	code = model.UpdateAddress(&data)
	if code == errmsg.ERROR {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}
